﻿using System;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Checkout : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //check to ensure the user is logged in
        if (Session["userID"] == null)
        { Response.Redirect("Default.aspx"); }

        OleDbConnection conn = null;
        OleDbDataReader auctionReader = null;
        OleDbDataReader winnerReader = null;
        string AuctionID = (Request.QueryString["AuctionID"]);
        string BuyNow = (Request.QueryString["BuyNow"]);
        string purchasePrice = null;

        //Set table widths.
        displayTable1.Width = Unit.Percentage(30.00);
        displayTable2.Width = Unit.Percentage(30.00);

        conn = new OleDbConnection(
            "Provider=Microsoft.ACE.OLEDB.12.0; " +
            "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
        conn.Open();

        OleDbCommand cmdAuctionInfo = new OleDbCommand("SELECT * FROM Auctions WHERE ID=" + AuctionID, conn);
        auctionReader = cmdAuctionInfo.ExecuteReader();
        auctionReader.Read();

        OleDbCommand cmdWinnerInfo = new OleDbCommand("SELECT * FROM Users WHERE ID='" + Session["userID"].ToString() + "'", conn);
        winnerReader = cmdWinnerInfo.ExecuteReader();
        winnerReader.Read();

        //Determine if this is a Buy Now Auction or time expired Auction and set the appropriate purchase price
        if (BuyNow == "Yes")
        {
            purchasePrice = auctionReader["buyNowPrice"].ToString();
            Session["purchasePrice"] = purchasePrice;
        }
        else
        {
            purchasePrice = auctionReader["currentBid"].ToString();
            Session["purchasePrice"] = purchasePrice;
        }

        //Title Row
        TableRow titleRow = new TableRow();
        TableHeaderCell titleTextCell = new TableHeaderCell();
        titleTextCell.Text = "Title:";
        titleTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell titleDataCell = new TableCell();
        titleDataCell.Text = auctionReader["title"].ToString();
        titleRow.Cells.Add(titleTextCell);
        titleRow.Cells.Add(titleDataCell);

        //Seller Row
        TableRow sellerRow = new TableRow();
        TableHeaderCell sellerTextCell = new TableHeaderCell();
        sellerTextCell.Text = "Seller:";
        sellerTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell sellerDataCell = new TableCell();
        sellerDataCell.Text = auctionReader["seller"].ToString();
        sellerRow.Cells.Add(sellerTextCell);
        sellerRow.Cells.Add(sellerDataCell);

        //Purchase Price Row
        TableRow purchasePriceRow = new TableRow();
        TableHeaderCell purchasePriceTextCell = new TableHeaderCell();
        purchasePriceTextCell.Text = "Purchase Price:";
        purchasePriceTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell purchasePriceDataCell = new TableCell();
        purchasePriceDataCell.Text = "$" + purchasePrice;
        purchasePriceRow.Cells.Add(purchasePriceTextCell);
        purchasePriceRow.Cells.Add(purchasePriceDataCell);

        //First Name Row
        TableRow firstNameRow = new TableRow();
        TableHeaderCell firstNameTextCell = new TableHeaderCell();
        firstNameTextCell.Text = "First Name:";
        firstNameTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell firstNameDataCell = new TableCell();
        firstNameDataCell.Text = winnerReader["firstName"].ToString();
        firstNameRow.Cells.Add(firstNameTextCell);
        firstNameRow.Cells.Add(firstNameDataCell);

        //Last Name Row
        TableRow lastNameRow = new TableRow();
        TableHeaderCell lastNameTextCell = new TableHeaderCell();
        lastNameTextCell.Text = "Last Name:";
        lastNameTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell lastNameDataCell = new TableCell();
        lastNameDataCell.Text = winnerReader["lastName"].ToString();
        lastNameRow.Cells.Add(lastNameTextCell);
        lastNameRow.Cells.Add(lastNameDataCell);

        //Address Row
        TableRow addressRow = new TableRow();
        TableHeaderCell addressTextCell = new TableHeaderCell();
        addressTextCell.Text = "Address:";
        addressTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell addressDataCell = new TableCell();
        addressDataCell.Text = winnerReader["addressNumber"].ToString() + " " + winnerReader["street"].ToString();
        addressRow.Cells.Add(addressTextCell);
        addressRow.Cells.Add(addressDataCell);

        //City Row
        TableRow cityRow = new TableRow();
        TableHeaderCell cityTextCell = new TableHeaderCell();
        cityTextCell.Text = "City:";
        cityTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell cityDataCell = new TableCell();
        cityDataCell.Text = winnerReader["city"].ToString();
        cityRow.Cells.Add(cityTextCell);
        cityRow.Cells.Add(cityDataCell);

        //State Row
        TableRow stateRow = new TableRow();
        TableHeaderCell stateTextCell = new TableHeaderCell();
        stateTextCell.Text = "State:";
        stateTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell stateDataCell = new TableCell();
        stateDataCell.Text = winnerReader["state"].ToString();
        stateRow.Cells.Add(stateTextCell);
        stateRow.Cells.Add(stateDataCell);

        //Country Row
        TableRow countryRow = new TableRow();
        TableHeaderCell countryTextCell = new TableHeaderCell();
        countryTextCell.Text = "Country:";
        countryTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell countryDataCell = new TableCell();
        countryDataCell.Text = winnerReader["country"].ToString();
        countryRow.Cells.Add(countryTextCell);
        countryRow.Cells.Add(countryDataCell);

        //Zip Code Row
        TableRow zipRow = new TableRow();
        TableHeaderCell zipTextCell = new TableHeaderCell();
        zipTextCell.Text = "Zip Code:";
        zipTextCell.HorizontalAlign = HorizontalAlign.Left;
        TableCell zipDataCell = new TableCell();
        zipDataCell.Text = winnerReader["zip"].ToString();
        zipRow.Cells.Add(zipTextCell);
        zipRow.Cells.Add(zipDataCell);

        //Close the readers
        auctionReader.Close();
        winnerReader.Close();

        //Close the DB connection
        conn.Close();

        displayTable1.Rows.Add(titleRow);
        displayTable1.Rows.Add(sellerRow);
        displayTable1.Rows.Add(purchasePriceRow);

        displayTable2.Rows.Add(firstNameRow);
        displayTable2.Rows.Add(lastNameRow);
        displayTable2.Rows.Add(addressRow);
        displayTable2.Rows.Add(cityRow);
        displayTable2.Rows.Add(stateRow);
        displayTable2.Rows.Add(countryRow);
        displayTable2.Rows.Add(zipRow);
    }

    protected void cmdSubmit(object sender, EventArgs e)
    {
        int AuctionID = int.Parse(Request.QueryString["AuctionID"]);
        string userID = Session["userID"].ToString();
        string dbUpdate = null;
        int purchasePrice = int.Parse(Session["purchasePrice"].ToString());
        string closed = "yes";
        string paid = "yes";
        

        if (TextBox1.Text == "")
        {
            string ScriptString1 = "alert('You must enter the name on the credit card.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }

        if (DropDownList1.Text == "Select Card Type")
        {
            string ScriptString1 = "alert('You must select the type of credit card.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }

        if (cardNumber.Text == "")
        {
            string ScriptString1 = "alert('You must enter a valid card number.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }

        double cN = Convert.ToDouble(cardNumber.Text);

        if (cN < 4000000000000000  || cN > 6011999999999999)
        {
            string ScriptString1 = "alert('You must enter a valid card number.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }

        if (DropDownList2.Text == "Select Expiration Month")
        {
            string ScriptString1 = "alert('You must select the month of expiration.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }

        if (DropDownList3.Text == "Select Expiration Year")
        {
            string ScriptString1 = "alert('You must select the year of expiration.');";
            System.Web.UI.Page page1 = HttpContext.Current.Handler as System.Web.UI.Page;
            page1.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString1, true);
        }

        OleDbConnection conn = null;

        conn = new OleDbConnection(
            "Provider=Microsoft.ACE.OLEDB.12.0; " +
            "Data Source=" + Server.MapPath("App_Data/BayouFarmersDB.accdb"));
        conn.Open();
        
        //Build the Update string
        dbUpdate = "UPDATE Auctions SET ";
        dbUpdate += "currentWinner=@currentWinner, closed=@closed, currentBid=@currentBid, paid=@paid ";
        dbUpdate += "WHERE ID=@AuctionID";

        //Create the Write command
        OleDbCommand cmdWrite = new OleDbCommand(dbUpdate, conn);

        //Add the Parameters
        cmdWrite.Parameters.AddWithValue("@currentWinner", Session["userID"]);
        cmdWrite.Parameters.AddWithValue("@closed", closed);
        cmdWrite.Parameters.AddWithValue("@currentBid", purchasePrice);
        cmdWrite.Parameters.AddWithValue("@paid", paid);
        cmdWrite.Parameters.AddWithValue("@AuctionID", AuctionID);



        //Write the new data to the DB
        cmdWrite.ExecuteNonQuery();

        //Close the DB connection
        conn.Close();

        //Display message notifying user that their bid was successful
        string ScriptString = "alert('Congratulations! You have won the Auction!');";
        ScriptString += "window.location='Default.aspx';";
        System.Web.UI.Page page = HttpContext.Current.Handler as System.Web.UI.Page;
        page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ClientScript", ScriptString, true);
    }

    protected void exitBtn_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
}